/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-01-05
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-01-06
 * @Description: 聊天页面
 */
import 'package:flutter/material.dart';

class ChatScreen extends StatefulWidget {
  final String user; // 传递聊天的用户

  const ChatScreen({Key? key, required this.user}) : super(key: key);

  @override
  _ChatScreenState createState() => _ChatScreenState();
}

class _ChatScreenState extends State<ChatScreen> {
  final TextEditingController _controller = TextEditingController();
  final List<String> _messages = [];

  // 发送消息的方法
  void _sendMessage() {
    if (_controller.text.isNotEmpty) {
      setState(() {
        _messages.add(_controller.text); // 将新消息添加到消息列表
      });
      _controller.clear(); // 清空输入框
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Chat with ${widget.user}'),
        actions: [
          IconButton(
            icon: Icon(Icons.more_vert),
            onPressed: () {
              // 更多设置功能
            },
          ),
        ],
      ),
      body: Column(
        children: [
          // 显示聊天消息的部分
          Expanded(
            child: ListView.builder(
              reverse: true, // 最新消息显示在底部
              itemCount: _messages.length,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Align(
                    alignment: index % 2 == 0
                        ? Alignment.centerLeft
                        : Alignment.centerRight,
                    child: Container(
                      padding: EdgeInsets.all(10),
                      margin: EdgeInsets.symmetric(vertical: 5),
                      decoration: BoxDecoration(
                        color: index % 2 == 0
                            ? Colors.grey[300]
                            : Colors.blueAccent,
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Text(
                        _messages[index],
                        style: TextStyle(
                            color:
                                index % 2 == 0 ? Colors.black : Colors.white),
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
          // 输入消息框
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Row(
              children: [
                // 输入框
                Expanded(
                  child: TextField(
                    controller: _controller,
                    decoration: InputDecoration(
                      hintText: 'Type a message...',
                      border: OutlineInputBorder(),
                    ),
                  ),
                ),
                // 发送按钮
                IconButton(
                  icon: Icon(Icons.send),
                  onPressed: _sendMessage,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
